<template>
  <div class='container'>
    <el-dialog
      :title="this.modifyData.id ? '修改目录' : '新增目录'"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属学科" v-show="!$route.query.id">
          <el-select
            clearable
            v-model="form.subjectID"
            placeholder="请选择"
            style="width:100%">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.subjectName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="form.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="hQuery">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入list-学科列表
import { list } from '@/api/hmmm/subjects'
// add-目录添加 update-修改目录 detail-目录详情
import { add, detail, update } from '@/api/hmmm/directorys'
export default {
  name: 'directorysAdd',
  // modifyData-当前修改数据
  props: {
    modifyData: {
      type: Object
    }
  },
  data () {
    return {
      dialogVisible: false,
      form: {
        subjectID: null,
        directoryName: ''
      },
      rules: {
        // subjectID: [
        //   { required: true, message: '请选择学科名称', trigger: 'change' }
        // ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ]
      },
      options: [],
      value: true
    }
  },
  created () {
    this.getSubject()
  },
  watch: {
    // 监听modifyData数据变化
    modifyData () {
      if (this.modifyData.id) {
        this.getDireList()
      }
    }
  },
  methods: {
    // 打开对话框
    openDialog () {
      this.dialogVisible = true
      this.form.subjectID = null
      this.form.directoryName = ''
      // 直接赋值空对象容易出现问题
      // this.form = {}
    },
    // 获取学科列表
    async getSubject () {
      try {
        const res = await list({ page: 1, pagesize: 100 })
        // console.log('学科列表', res)
        this.options = res.data.items
      } catch (error) {
        console.log(error)
      }
    },
    // 点击确认按钮
    hQuery () {
      // 全局表单校验
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // 调接口
          try {
            if (!this.modifyData.id) {
              this.form.subjectID = this.$route.query.id ? Number(this.$route.query.id) : this.form.subjectID
              if (this.form.subjectID) {
                // const id = this.$route.query.id ? Number(this.$route.query.id) : this.form.subjectID
                const data = {
                  subjectID: this.form.subjectID,
                  directoryName: this.form.directoryName
                }
                await add(data)
                // 提示
                this.$message.success('添加目录成功')
                this.dialogVisible = false
              } else {
                this.$message.warning('请选择所属学科')
              }
            } else {
              this.edit()
              this.$message.success('修改目录成功')
              this.dialogVisible = false
              this.modifyData.id = null
            }
            // 刷新页面
            this.$emit('addDire')
          } catch (error) {
            console.log(error)
            this.$message.error('操作失败')
          }
        }
      })
    },
    // 获取文章详情
    async getDireList () {
      // 获取目录详情
      const res = await detail(this.modifyData)
      console.log('目录详情', res)
      this.form = res.data
    },
    // 修改目录
    async edit () {
      try {
        // 修改目录
        await update(this.form)
        console.log('修改')
        this.$emit('updateDire', this.form)
      } catch (error) {
        console.log(error)
        this.$message.error('修改目录失败')
      }
    }
  }
}
</script>

<style scoped lang='less'></style>
